package be.redo.model.entities;

import java.io.Serializable;
import java.math.BigDecimal;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.format.annotation.NumberFormat;

/**
 * @author Frank Plain old Java class to represent a database table row.
 *         Represents the place where a customer stays during (a part of
 *         his/her) vacation in the domain model.
 * 
 */
@Entity
@NamedQueries({
		@NamedQuery(name = "findAantalResidences", query = "select count(r) from Residence r"),
		@NamedQuery(name = "findAllResidences", query = "select r from Residence r order by r.name"),
		@NamedQuery(name = "findResidencesByRegion", query = "select r from Residence r where r.region =  :region "),
		@NamedQuery(name = "findResidencesByNameLike", query = "select r from Residence r where r.name like( :name )") })
public class Residence implements Serializable, Comparable<Residence> {

	/**
	 * Generated serialVersionUID.
	 */
	private static final long serialVersionUID = 6109933827400692011L;

	/**
	 * Name of the Residence.
	 */
	@NotNull
	@Size(min = 1, max = 2550)
	private String name;

	/**
	 * Price for 1 person and 1 night in euros.
	 */
	@NotNull
	@Min(0)
	@NumberFormat(pattern = "#,##0.00")
	private BigDecimal price;

	/**
	 * geographical region of the residence.
	 */
	@NotNull
	@ManyToOne
	@JoinColumn(name = "region")
	private Region region;

	@Override
	public String toString() {
		return ReflectionToStringBuilder.toString(this,
				ToStringStyle.SHORT_PREFIX_STYLE);
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public BigDecimal getPrice() {
		return this.price;
	}

	public void setPrice(BigDecimal price) {
		this.price = price;
	}

	public Region getRegion() {
		return this.region;
	}

	public void setRegion(Region region) {
		this.region = region;
	}

	/**
	 * Generated key by the Database
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "residence_id")
	private Long ResidenceId;

	public Long getResidenceId() {
		return this.ResidenceId;
	}

	public void setResidenceId(Long id) {
		this.ResidenceId = id;
	}

	/**
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((region == null) ? 0 : region.hashCode());
		return result;
	}

	/**
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Residence other = (Residence) obj;
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		} else if (!name.equals(other.name)) {
			return false;
		}
		if (region == null) {
			if (other.region != null) {
				return false;
			}
		} else if (!region.equals(other.region)) {
			return false;
		}
		return true;
	}

	@Override
	public int compareTo(Residence o) {
		return price.compareTo(o.price);
	}

}
